<template>
  <div>
    <Head />
    <div class="all">
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="100px"
 
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="所属分类" prop="cate">
          <el-select v-model="form.cate" placeholder="请选择所属分类">
            <el-option
              v-for="(item, index) in cateList"
              :key="index"
              :label="item.cate_name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author"></el-input>
        </el-form-item>

        <el-form-item label="详情"  prop="content">
          <div id="info"></div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="isSubmit" @click="submitForm('form')"
            >立即发布</el-button
          >
          <el-button :loading="isSubmit" @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Head from "../components/Head.vue";
import E from "wangeditor";

import { addBlog, getCate } from "@/http/api/blog";
export default {
  data() {
    return {
      editor: null,
      isSubmit: false,
      cateList: [],
      form: {
        title: "",
        content: "",
        author: "",
        cate: "",
      },
      rules: {
        title: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
        cate: [
          { required: true, message: "请选择所属分类", trigger: "change" },
        ],
        author: [{ required: true, message: "请选择作者", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getData();
  },
  mounted() {  
    this.editor = new E("#info");
     // 配置 server 接口地址
    this.editor.config.uploadImgServer = '/upload-img'
    this.editor.create();
   this.form = JSON.parse(localStorage.getItem("addform"));
    this.editor.txt.html(this.form.content);
  },
  methods: {
    getContent() {
      this.isSubmit = true;
      this.form.content = this.editor.txt.html();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.getContent();
          this.form.content = this.editor.txt.html();
          addBlog(this.form).then((res) => {
            if (res.code === 0) {
             this.$message({
                message: res.msg,
                duration: 2000,
                type: "success",
              });
              setTimeout(() => {
                this.$router.push({ name: "home" });
              }, 2001);
            }
            this.isSubmit =false;
          });
        } else {
 
          return false;
        }
      });
    },
    resetForm(formName){
      this.$refs[formName].resetFields();
      this.form.content =""
      this.editor.txt.clear()
    },
    getData() {
      getCate().then((res) => {
        if (res.code === 0) {
          this.cateList = [...res.data];
        }
      });
    },
  },
  components: { Head },
};
</script>

<style scoped lang='scss'>
@import "../theme/style.scss";
.all {
  margin: 0 auto;
  width:$content_w;
  margin-top: 25px;
}
</style>